package hiep.homework4;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private SinhVienDataSource datasource;
	ListView lv = null;
	EditText editName, editAge, editCountry;
	List<SinhVienEntity> values;
	TextView tvSearch;
	int vitri;
	ArrayAdapter<SinhVienEntity> adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		lv = (ListView) findViewById(android.R.id.list);
		editName = (EditText) findViewById(R.id.edtName);
		editAge = (EditText) findViewById(R.id.edtAge);
		editCountry = (EditText) findViewById(R.id.edtCountry);
		tvSearch = (TextView) findViewById(R.id.tvSearch);

		datasource = new SinhVienDataSource(this);
		datasource.open();
		values = datasource.getAllSinhVien();

		adapter = new ArrayAdapter<SinhVienEntity>(MainActivity.this,
				android.R.layout.simple_list_item_1, values);

		MyArrayAdapter adapter2 = new MyArrayAdapter(MainActivity.this,
				R.layout.layout_item_sinhvien, values);
		lv.setAdapter(adapter2);
		lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				vitri = arg2;
				tvSearch.setText(values.get(arg2).getId() + "");
				editName.setText(values.get(arg2).getName());
				editAge.setText(values.get(arg2).getAge() + "");
				editCountry.setText(values.get(arg2).getCountry());
			}
		});
	}

	public void AddSinhVien(View v) {
		if (editName.getText() + "" == "" || editAge.getText() + "" == ""
				|| editCountry.getText() + "" == "") {
			Toast.makeText(MainActivity.this, "Hãy điền đầy đủ thông tin",
					Toast.LENGTH_LONG).show();
		} else {
			String name = editName.getText() + "";
			int age = Integer.parseInt(editAge.getText() + "");
			String country = editCountry.getText() + "";

			SinhVienEntity sinhVienEntity = new SinhVienEntity();
			sinhVienEntity = datasource.createSinhVien(name, age, country);
			adapter.add(sinhVienEntity);

			Toast.makeText(MainActivity.this,
					"Thêm thành công: " + sinhVienEntity.getName(),
					Toast.LENGTH_LONG).show();
			MyArrayAdapter adapter2 = new MyArrayAdapter(MainActivity.this,
					R.layout.layout_item_sinhvien, values);
			lv.setAdapter(adapter2);
			adapter.notifyDataSetChanged();
		}
	}

	public void UpdateSinhVien(View v) {
		if (editName.getText() + "" == "" || editAge.getText() + "" == ""
				|| editCountry.getText() + "" == "") {
			Toast.makeText(MainActivity.this, "Hãy điền đầy đủ thông tin",
					Toast.LENGTH_LONG).show();
		} else {
			String name = editName.getText() + "";
			int age = Integer.parseInt(editAge.getText() + "");
			String country = editCountry.getText() + "";
			SinhVienEntity sinhVienEntity = new SinhVienEntity();
			sinhVienEntity = (SinhVienEntity) values.get(vitri);
			sinhVienEntity.setName(name);
			sinhVienEntity.setAge(age);
			sinhVienEntity.setCountry(country);
			datasource.updateSinhVien(sinhVienEntity);
			Toast.makeText(MainActivity.this,
					"Cập nhật thành công: " + sinhVienEntity.getName(),
					Toast.LENGTH_LONG).show();
			MyArrayAdapter adapter = new MyArrayAdapter(MainActivity.this,
					R.layout.layout_item_sinhvien, values);
			lv.setAdapter(adapter);
			adapter.notifyDataSetChanged();
		}
	}

	public void DeleteSinhVien(View v) {
		SinhVienEntity sinhVienEntity = new SinhVienEntity();

		if (values.size() > 0) {
			sinhVienEntity = (SinhVienEntity) values.get(vitri);
			datasource.deleteSinhVien(sinhVienEntity);
			adapter.remove(sinhVienEntity);
		}
		MyArrayAdapter adapter2 = new MyArrayAdapter(MainActivity.this,
				R.layout.layout_item_sinhvien, values);
		lv.setAdapter(adapter2);
		adapter.notifyDataSetChanged();
		Toast.makeText(MainActivity.this,
				"Xóa thành công: " + sinhVienEntity.getName(),
				Toast.LENGTH_LONG).show();
	}

	public void searchSV(View v) {
		String name = editName.getText() + "";
		values = datasource.searchSVbyName(name);
		MyArrayAdapter adapter2 = new MyArrayAdapter(MainActivity.this,
				R.layout.layout_item_sinhvien, values);
		lv.setAdapter(adapter2);
		adapter.notifyDataSetChanged();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
